<?php
// echo $this->ckeditor->editor("my_editor");
$config['toolbar'] = array(
    array( 'Source','-',
          'NewPage','Preview','Templates','-',
          'Cut','Copy','Paste','PasteText','PasteFromWord','-',
          'Undo','Redo','-',
          'Find','Replace','-',
          'SelectAll','RemoveFormat','-',
          'Maximize', 'ShowBlocks'),
    '/',
    array('Bold','Italic','Underline','Strike','-',
          'Subscript','Superscript','-',
          'NumberedList','BulletedList','-',
          'Outdent','Indent','Blockquote','-',
          'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-',
          'Link','Unlink','Anchor','-',
          'Image','Flash','Table','HorizontalRule','SpecialChar'
          ),
    '/',
    array('Format','Font','FontSize','-',
          'TextColor','BGColor', 'MediaEmbed')
);      
        
$config['height'] = 500;

 //$fckEditor = fckEditor('txtViDescription', $vi_description);
  $fckEditor = '';// $this->ckeditor->editor("my_editor", "default value", $config);
 //$CI->fckeditor->CreateHtml() ;
?>
  <?php  $this->load->view('admin/product/link_bar');?>
  <script type="text/javascript" src="<?php echo base_url('js/jquery1.6.1.js')?>" ></script>
<script type="text/javascript" src="<?php echo base_url('js/ajaxupload.3.5.js')?>" ></script>


<style>
    #upload{
        margin:30px 0px; padding:5px;
        font-weight:bold; font-size:1.3em;
        font-family:Arial, Helvetica, sans-serif;
        text-align:center;
        background:#f2f2f2;
        color:#3366cc;
        border:1px solid #ccc;
        width:150px; height: 20px;
        cursor:pointer !important;
        -moz-border-radius:5px; -webkit-border-radius:5px;
    }
    .darkbg{
        background:#ddd !important;
    }
    #status{
        font-family:Arial; padding:5px;
    }
    div#files{ list-style:none; padding:0; margin:0; }
    div#files div{ padding:5px; margin-bottom:2px; width:120px; float:left; margin-right:5px;}
    div#files div img{ max-width:100px; max-height:100px; }
    .success{ background:#99f099; border:1px solid #339933; }
    .error{ background:#f0c6c3; border:1px solid #cc6622; }
</style>
<script type="text/javascript" >
    function deleteImage(image_id, image_name, consultant_id){
        var url_request = "<?php echo site_url('admin/product/delete_image/'); ?>";
        $.ajax({
            type: "POST",
            url: url_request ,
            data: "product_id=" + consultant_id + "&image_name="+ image_name ,
            dataType: "json",
            success: function(msg){
                //if(msg.error == false)
                $('#' + image_id).hide(1);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {

                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: Delete image not successfull." + error.empty);
            }
        });
    }
    
    $(function(){
        var btnUpload=$('#upload');
        var status=$('#status');
        var dateAdd = $('#txtDateAdd').val();
        new AjaxUpload(btnUpload, {
            action: '<?php echo site_url('admin/product/do_upload/');?>/{id_product}',
            name: 'uploadfile',
            data: {
                date_add : dateAdd,
                // example_key2 : 'example_value2'
            },
            onSubmit: function(file, ext){
                if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
                    // extension is not allowed
                    status.text('Only JPG, PNG or GIF files are allowed');
                    return false;
                }
                status.text('Uploading...');
            },
        onComplete: function(file, response){
            //On completion clear the status
            status.text(response);
            //return;
            
                //alert(response);

                var msg = jQuery.parseJSON(response);
                
                //return;
                if(response=="error"){
                    //alert("error ");
                    alert(msg.upload_dir);
                    $('<li></li>').appendTo('#files').text(file).addClass('error');
                }
                else{
                	alert(msg.upload_dir);
                    $('<div class ="success" id="'+msg.image_id+'"></div>').appendTo('#files').html(
                    '<img src="{base_url}'+ msg.upload_dir + "/" + 
                        msg.filename+'" alt="" height=65 width=65 /><br />'+
                        '<a href="javascript:void();" onclick="insert(\''+msg.filename+ '\', \'vi\');">Insert (Vi)</a>' +
                      '<a href="javascript:void();" onclick="insert(\''+msg.filename+ '\', \'en\');">Insert (En)</a>' +
                        ' <a href="javascript:void();" onclick="setImageDefault(\''+msg.filename+ '\', \'{id_product}\');">Default</a>' +
                        ' <a href="javascript:void();" onclick="deleteImage('+msg.image_id+ ',\''+msg.filename+ '\', \'{id_news}\')">Delete</a>'
                );
                }
            //else $('<li></li>').appendTo('#files').text(response).addClass('error');
        }
    });

});
</script>


<script type = "text/javascript">

   function setImageDefault(image_name, consultant_id){
        var url_request = "<?php echo site_url('admin/product/set_image/'); ?>";
        $.ajax({
            type: "POST",
            url: url_request ,
            data: "product_id=" + consultant_id + "&image_name="+ image_name ,
            dataType: "json",
            success: function(msg){
                //if(msg.error == false)
                //    $('#' + province_id).hide(1);

            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: " + textStatus.toString() + url_request);
            }
        }).done(function ( data ) { 
        	 alert(data);
        });
    }
    
</script>


 <div class="box_function">

<?php //echo $legend_text ?>
<?php echo $error_message?>
<form action="<?php echo site_url('admin/product/save')?>" method="post" enctype="multipart/form-data">
	 <table width="747" border="0" cellpadding="0" cellspacing="0" class="listproduct" id="listproduct">
	
	<tr>
		
		<td class="product">
		<div>
		<?php echo lang('product_code')?> 
		</div>
		<input type="text" class="text" name="txtCode" id="txtCode" value="<?php echo $code?>" style="width: 150px;" maxlength="60" /> <?php echo lang('invalid_characters')?>
		</td>
	</tr>
	<tr>
		
		<td class="product">
		<div>
		<?php echo lang('category')?>
		</div>
		<?php echo $ddlCategory?>
		</td>
	</tr>
	<tr>
		
		<td class="product">
		<div>
		<?php echo $this->lang->line('vi_name')?>
		</div>
		<input type="text" style="width: 260px; height:20px" class="text" name="txtViName" id="txtViName" value="<?php echo $vi_name?>" maxlength="100" />
		</td>
	</tr>
	<tr>
		
		<td class="product">
		<div>
		<?php echo lang('en_name')?>
		</div>
		<input type="text" style="width: 260px; height:20px" class="text" name="txtEnName" id="txtEnName" value="<?php echo $en_name?>" maxlength="100" />
		</td>
	</tr>
	<tr>
		
		<td class="product">
		<div>
		<?php echo lang('vi_description')?>
		</div>
		<?php
		echo  $this->ckeditor->editor("txtViDescription", $vi_description, $config) ;
		// echo fckEditor('txtViDescription', $vi_description);?>
		
		</td>
	</tr>
	<tr>
		
		<td class="product">
		<div>
		<?php echo lang('en_description')?>
		</div>
		<?php 
			//$fckEditor->InstanceName = 'txtEnDescription';
			//$fckEditor->Value = $en_description;
			//echo $fckEditor->CreateHtml() ;
			//echo fckEditor('txtEnDescription', $en_description);
                        echo  $this->ckeditor->editor("txtEnDescription", $en_description, $config) ;
                ?>
		</td>
	</tr>
	
	
	 <tr>
                <td style="padding: 5px" colspan="2">
                    <h5>Hình ảnh:</h5>
                    <script>
                    function insert(file, location){
                       // oFormObject = document.forms['myform'];

                        //CKEDITOR.instances['instanceName'].insertHTML('&lt; img src="http://localhost/vntsolution/images/admin/add.gif"/&gt;');

                        //var imgHtml = CKEDITOR.dom.element.createFromHtml("<img align='absmiddle' src='http://localhost/vntsolution/images/admin/add.gif'>");
                        //CKEDITOR.instances.body.insertElement(imgHtml);
    
                        //oFormObject.elements["txtViDescription"].value = '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">' + oFormObject.elements["txtViDescription"].value;
                        //oFormObject.elements["txtViDescription"].value = '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">';
                        //alert("sdsd");
                        //document.getElementById("listimage").innerHTML =  '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">';
                        //var writer = new CKEDITOR.htmlWriter();
                        //writer.openTag( '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">' );
                        if(location == 'vi')
                            CKEDITOR.instances.txtViDescription.insertHtml("<img align='absmiddle' src='{base_url}{upload_dir}/"+file+"'>");
                        else
                        CKEDITOR.instances.txtEnDescription.insertHtml("<img align='absmiddle' src='{base_url}{upload_dir}/"+file+"'>");
                    }
                    </script>
                                       
                    <div id="files" >
                    {images}
                     <div class ="success" id="{image_id}"> <img src="{base_url}{upload_dir}/{vi_file_name}" alt="" /><br />
                         <a href="javascript:void();" onclick="insert('{vi_file_name}', 'vi');">Insert (Vi)</a>
                         <a href="javascript:void();" onclick="insert('{vi_file_name}', 'en');">Insert (En)</a>
                         <a href="javascript:void();" onclick="setImageDefault('{vi_file_name}', {id_product});">Default</a>
                         <a href="javascript:void();" onclick="deleteImage({image_id},'{vi_file_name}', {id_product})">Delete</a>
                    </div>
                    {/images}
                    </div>
                    
                    <div style="clear:both"></div>
                    <div id="upload" ><span>Upload File<span></div>
                                <span id="status" ></span>

                                </td>
                                </tr>
	<tr>
		
		<td class="product">
		<table width="90%">
			<tr>
				<td>
					<div>
		<?php echo lang('unit')?>
		</div>
		<select name="unit">
		<?php
			for($i = 0; $i < count($units); $i++){
				if($units[$i]->id == $id_unit)
					echo '<option selected value='.$units[$i]->id.'>'. $units[$i]->vi_name . '</option>';
				else
					echo '<option value='.$units[$i]->id.'>'. $units[$i]->vi_name . '</option>';
			}
		?>
		</select>
				</td>
				
				<td>
					<div>
		<?php echo lang('source')?>
		</div>
		<select name="source">
		<?php
			for($i = 0; $i < count($sources); $i++){ 
				if($sources[$i]->id == $id_source)
					echo '<option selected="selected" value='.$sources[$i]->id.'>'. $sources[$i]->vi_name . '</option>';
				else
					echo '<option value='.$sources[$i]->id.'>'. $sources[$i]->vi_name . '</option>';
			}
		?>
		
		</select>
				</td>
				
				<td>
					<div>
		<?php echo lang('provider')?> 
		</div>
		<select name="provider">
		<?php
			for($i = 0; $i < count($providers); $i++){ 
				if($providers[$i]->id_provider == $id_provider)
					echo '<option selected="selected" value='.$providers[$i]->id_provider.'>'. $providers[$i]->vi_name . '</option>';
				else
					echo '<option value='.$providers[$i]->id_provider.'>'. $providers[$i]->vi_name . '</option>';
			}
		?>
		
		</select>
				</td>
				
			</tr>
		</table>
		
		</td>
	</tr>
	
	<tr>
		
		<td class="product">
		<table width="90%">
		<tr>
		<td class="product">
		<div>
		<?php echo lang('quantity')?>
		</div>
		<input type="text" style="width: 160px; height:20px" class="text" name="txtQuantity" id="txtQuantity" value="<?php echo $quantity?>" maxlength="200" />
		</td>
		
		<td>
			<div>
		<?php echo lang('price')?>
		</div>
		<input type="text" style="width: 160px; height:20px" class="text" name="txtPrice" id="txtPrice" value="<?php echo $price?>" maxlength="200" />
		</td>
		<td>
			<div>
		<?php echo lang('old_price')?>
		</div>
		<input type="text" style="width: 160px; height:20px" class="text" name="txtOldPrice" id="txtOldPrice" value="<?php echo $old_price?>" maxlength="200" />
		</td>
		<td>
			<div>
		Sale off
		</div>
		<input type="text" style="width: 160px; height:20px" class="text" name="txtSaleOff" id="txtSaleOff" value="<?php echo $sale_off?>" maxlength="200" />
		</td>
		</tr>
		</table>
		
		</td>
	</tr>
	<tr style="display:none">
		
		<td class="product">
		<div>
		<?php echo lang('image')?>
		</div>
		<?php 
		if(!empty($image))
			echo '<img src="'.base_url().PRODUCT_IMAGE_PATH.$image.'" height=90 width=90><br>';
		?>
		<input type="file" style="width: 160px; height:20px" name="file" id="file"/>
		</td>
	</tr>
	
	<tr>
		<td class="product">
		
		<?php echo lang('best_sell_product')?>
		
		<input type="checkbox" class="checkbox" <?php echo $best_sell==1? 'checked' : ''?> name="bestSell" value="1" />
		&nbsp;&nbsp;&nbsp;&nbsp;

        <?php echo lang('special_product')?>

		<input type="checkbox" class="checkbox" <?php echo $special==1? 'checked' : ''?> name="special" value="1" />
		&nbsp;&nbsp;&nbsp;&nbsp;

		<?php echo lang('active')?>
		<input type="checkbox" class="checkbox" <?php echo $active==1? 'checked' : ''?> name="active" value="1" />
		</td>
	</tr>
		
	<tr>
		
		<td class="product">
		<?php echo lang('indicates_a_required_field');?>
 		</td>
	</tr>
	
</table>
<input type="hidden" name="txtID" id="txtID" value="<?php echo $id_product?>"/>
<input type="hidden" name="txtDateAdd" id="txtDateAdd" value="<?php echo $date_add?>"/>
  
<p> <input type="submit" class="submit button" name="btSave" value="<?php echo lang('save')?>" /> </p>
</form>

 </div>